//引入模块
//app模块：控制应用的生命周期
//BrowserWindow模块：创建一个游览器窗口
const { app, BrowserWindow, Menu } = require('electron')
const template = require("./menu")
const path = require('path')


// 全局变量
let win

// 初始化应用，触发监听ready事件
app.on('ready', zzxm_createWindow)

// 自定义方法，创建窗口
function zzxm_createWindow() {
    // 创建窗口，文档：【https://www.w3cschool.cn/electronmanual/electronmanual-browser-window.html】
    win = new BrowserWindow({
        width: 800,
        height: 600,
        icon: './favicon.ico',
        // autoHideMenuBar:true,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js')
        }
    })
    // 加载内容
    // 加载远程
    // win.loadURL("http://www.baidu.com")  
    // 加载本地
    // __dirname 加载本文件所在的文件夹路径【相对路径】
    // win.loadURL(path.join(__dirname, './index.html'))  
    win.loadURL(path.join(__dirname, '../electron1/dist/index.html'))  
    // win.loadFile('./dist/index.html')

    // 打开调试.
    // win.webContents.openDevTools()

    //关闭窗口.
    win.on('closed', function () {
        win = null
    })

    // 设置菜单
    const m = Menu.buildFromTemplate(template);
    Menu.setApplicationMenu(m);
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.


// Quit when all windows are closed.
app.on('window-all-closed', function () {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (win === null) createWindow()
})
